﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace B13_匿名函数
{
    class Program
    {

        public delegate int DelCompare(object o1, object o2);
        static void Main(string[] args)
        {
            string[] strings = { "heh", "hsadfaf", "a", "safsafafasfa", "sdd" };
            string str = (string)GetMax(strings, StringCompare);
            Console.WriteLine(str);

            object[] nums = { 1, 2, 5, 6, 22, 34, 2, 4, 5, 7, 9 };
            int max = (int)GetMax(nums, IntCompare);
            //匿名函数 1
            int max1 = (int)GetMax(nums, delegate (object o1, object o2) { return (int)o1 - (int)o2; });
            //匿名函数 2
            int max2 = (int)GetMax(nums, (object o1, object o2) => { return (int)o1 - (int)o2; });

            Console.WriteLine("{0}  {1}", max, max1);//打印的索引必须先从0开始，否则会抛出异常

            Console.ReadKey();
        }

        public static object GetMax(object[] objs, DelCompare comp)
        {
            object max = objs[0];

            for (int i = 1; i < objs.Length; ++i)
            {
                if (comp(max, objs[i]) < 0)
                {
                    max = objs[i];
                }
            }

            return max;
        }


        static int IntCompare(object o1, Object o2)
        {
            return (int)o1 - (int)o2;
        }

        static int StringCompare(object o1, object o2)
        {
            string s1 = (string)o1;
            string s2 = (string)o2;
            return s1.Length - s2.Length;
        }

    }
}
